<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_kalender_akademik extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.id_tahunajaran';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'desc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_pt = isset($_POST['kd_pt']) ? strval($_POST['kd_pt']) : '';
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_tahunajaran, a.Nama, b.Nama as nama_pt, c.nama_fakultas, d.nama_prodi');
			$this->db->from('tbl_tahunajaran a');
			$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.kd_fakultas');
			$this->db->join('ref_prodi d', 'd.kd_prodi = a.kd_prodi');
			
			#Kimochi - otoritas
				#--Faklutas
			if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('a.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('a.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			#End Kimochi - otoritas
			
			// if($kd_pt != '')
				// $this->db->where('a.kd_pt', $kd_pt);
				
			$this->db->where('a.status', 'A');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdata_detail($id){
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nama_matakuliah';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_matakuliah, b.nama_matakuliah, b.sks, b.semester');
			$this->db->from('tbl_krs_detail a');
			$this->db->join('ref_matakuliah b', 'b.kd_matakuliah = a.kd_matakuliah');
			$this->db->where('a.id_krs', $id);
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function get_all_prodi($kd_fakultas){
		$this->db->flush_cache();
		$this->db->select('kd_prodi');
		$this->db->from('ref_prodi');
		$this->db->where('kd_fakultas', $kd_fakultas);
		
		return $this->db->get();
		
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('id_tahunajaran', $kode['id_tahunajaran'].$kode['semester']);
		
		$this->db->select('*');
		$this->db->from('tbl_tahunajaran');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function getdataedit($id){
		$this->db->flush_cache();
		$this->db->where('id_tahunajaran', $id);
		return $this->db->get('tbl_tahunajaran');
	}
	
	function InsertOnDb($data){
		$this->db->trans_start();
		
		foreach($data['all_prodi']->result() as $row){
			
			//query insert data		
			$this->db->flush_cache();
			$this->db->set('id_tahunajaran', $data['id_tahunajaran'].$data['semester']);
			$this->db->set('kd_pt', $data['kd_pt']);
			$this->db->set('kd_fakultas', $data['kd_fakultas']);
			$this->db->set('kd_prodi', $row->kd_prodi);
			$this->db->set('Nama', $data['Nama']);
			
			$this->db->set('TglKRSMulai', FormatDateToMysql($data['TglKRSMulai']));
			$this->db->set('TglKRSSelesai', FormatDateToMysql($data['TglKRSSelesai']));
			$this->db->set('TglKRSOnlineMulai', FormatDateToMysql($data['TglKRSOnlineMulai']));
			$this->db->set('TglKRSOnlineSelesai', FormatDateToMysql($data['TglKRSOnlineSelesai']));
			$this->db->set('TglUbahKRSMulai', FormatDateToMysql($data['TglUbahKRSMulai']));
			$this->db->set('TglUbahKRSSelesai', FormatDateToMysql($data['TglUbahKRSSelesai']));
			$this->db->set('TglCetakKSS1', FormatDateToMysql($data['TglCetakKSS1']));
			$this->db->set('TglCetakKSS2', FormatDateToMysql($data['TglCetakKSS2']));
			$this->db->set('TglCuti', FormatDateToMysql($data['TglCuti']));
			$this->db->set('TglMundur', FormatDateToMysql($data['TglMundur']));
			$this->db->set('TglBayarMulai', FormatDateToMysql($data['TglBayarMulai']));
			$this->db->set('TglBayarSelesai', FormatDateToMysql($data['TglBayarSelesai']));
			$this->db->set('TglKembaliUangKuliah', FormatDateToMysql($data['TglKembaliUangKuliah']));
			$this->db->set('TglKuliahMulai', FormatDateToMysql($data['TglKuliahMulai']));
			$this->db->set('TglKuliahSelesai', FormatDateToMysql($data['TglKuliahSelesai']));
			$this->db->set('TglUTSMulai', FormatDateToMysql($data['TglUTSMulai']));
			$this->db->set('TglUTSSelesai', FormatDateToMysql($data['TglUTSSelesai']));
			$this->db->set('TglUASMulai', FormatDateToMysql($data['TglUASMulai']));
			$this->db->set('TglUASSelesai', FormatDateToMysql($data['TglUASSelesai']));
			$this->db->set('TglNilai', FormatDateToMysql($data['TglNilai']));
			$this->db->set('TglAkhirKSS', FormatDateToMysql($data['TglAkhirKSS']));
			
			$this->db->set('SP', $data['semester']=='3'?'Y':'N');
			
			$this->db->set('Catatan', $data['Catatan']);
			
			$this->db->set('CUID', $this->session->userdata('user_id'));
			$this->db->set('CDATE', date('Y-m-d H:i:s'));
			//$this->db->set('MUID', $data['MUID']);
			//$this->db->set('MDATE', $data['MDATE']);
			
			$this->db->set('Status', isset($data['Status'])?'A':'D');

			$result = $this->db->insert('tbl_tahunajaran');
		}	
		
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function UpdateOnDb($data){
		//query update data		
		$this->db->flush_cache();
		$this->db->set('id_tahunajaran', $data['id_tahunajaran'].$data['semester']);
        $this->db->set('kd_pt', $data['kd_pt']);
        $this->db->set('kd_fakultas', $data['kd_fakultas']);
        $this->db->set('kd_prodi', $data['kd_prodi']);
        $this->db->set('Nama', $data['Nama']);
		
        $this->db->set('TglKRSMulai', FormatDateToMysql($data['TglKRSMulai']));
        $this->db->set('TglKRSSelesai', FormatDateToMysql($data['TglKRSSelesai']));
        $this->db->set('TglKRSOnlineMulai', FormatDateToMysql($data['TglKRSOnlineMulai']));
        $this->db->set('TglKRSOnlineSelesai', FormatDateToMysql($data['TglKRSOnlineSelesai']));
        $this->db->set('TglUbahKRSMulai', FormatDateToMysql($data['TglUbahKRSMulai']));
        $this->db->set('TglUbahKRSSelesai', FormatDateToMysql($data['TglUbahKRSSelesai']));
        $this->db->set('TglCetakKSS1', FormatDateToMysql($data['TglCetakKSS1']));
        $this->db->set('TglCetakKSS2', FormatDateToMysql($data['TglCetakKSS2']));
        $this->db->set('TglCuti', FormatDateToMysql($data['TglCuti']));
        $this->db->set('TglMundur', FormatDateToMysql($data['TglMundur']));
        $this->db->set('TglBayarMulai', FormatDateToMysql($data['TglBayarMulai']));
        $this->db->set('TglBayarSelesai', FormatDateToMysql($data['TglBayarSelesai']));
        $this->db->set('TglKembaliUangKuliah', FormatDateToMysql($data['TglKembaliUangKuliah']));
        $this->db->set('TglKuliahMulai', FormatDateToMysql($data['TglKuliahMulai']));
        $this->db->set('TglKuliahSelesai', FormatDateToMysql($data['TglKuliahSelesai']));
        $this->db->set('TglUTSMulai', FormatDateToMysql($data['TglUTSMulai']));
        $this->db->set('TglUTSSelesai', FormatDateToMysql($data['TglUTSSelesai']));
        $this->db->set('TglUASMulai', FormatDateToMysql($data['TglUASMulai']));
        $this->db->set('TglUASSelesai', FormatDateToMysql($data['TglUASSelesai']));
        $this->db->set('TglNilai', FormatDateToMysql($data['TglNilai']));
        $this->db->set('TglAkhirKSS', FormatDateToMysql($data['TglAkhirKSS']));
		
        $this->db->set('SP', $data['semester']=='3'?'Y':'N');
		
        $this->db->set('Catatan', $data['Catatan']);
		
        //$this->db->set('CUID', $this->session->userdata('user_id'));
        //$this->db->set('CDATE', date('Y-m-d H:i:s'));
        $this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
        
		$this->db->set('Status', isset($data['Status'])?'A':'D');
	
		$this->db->where('id_tahunajaran', $data['kode']);
		$this->db->where('kd_pt', $data['kd_pt']);
		$this->db->where('kd_prodi', $data['kd_prodi']);
		
        $result = $this->db->update('tbl_tahunajaran');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($id){
		//query delete data		
		$this->db->flush_cache();
		$this->db->set('Status', 'D');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_tahunajaran', $id);
		
		$result = $this->db->update('tbl_tahunajaran');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
}

?>